﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        long N0 = 0;        // 初始值
        float Rate = 0f;    // 比例值
        long Add = 0;       // 递增值
        long Add_Period = 0;// 递增值次数限定

        long Sub = 0;       // 递减值
        long Sub_Period = 0;// 递减值次数限定

        long Period = 0;    // 周期数

        long Result = 0;    // 结果值

        private void button1_Click(object sender, EventArgs e)
        {
            N0 = long.Parse(start.Text.Trim());
            Rate = float.Parse(rate.Text.Trim()) / 100;
            Add = long.Parse(add.Text.Trim());
            Add_Period = long.Parse(period_add.Text.Trim());

            Sub = long.Parse(sub.Text.Trim());
            Sub_Period = long.Parse(period_sub.Text.Trim());

            Period = long.Parse(period.Text.Trim());

            Result = F(N0, Rate, Add, Add_Period, Sub, Sub_Period, Period);
            result.Text = Result.ToString();
        }


        private long F(long N0, float Rate, long Add, long Add_Period, long Sub, long Sub_Period, long Period)
        {
            for (int p = 1; p <= Period; p++)
            {
                //long ResultBase = (long)(N0 * (1 + Rate));
                long ResultBase = 0;

                if (p > Add_Period && p - Add_Period <= Sub_Period)
                {
                    for (int i = 1; i <= 12; i++)
                    {
                        ResultBase += (long)((N0 - i * Sub) * (1 + Rate) / 12); // 按月递减
                    }
                }
                else ResultBase = (long)(N0 * (1 + Rate));

                long ResultAdd = 0;
                if (p <= Add_Period)
                {
                    for (int i = 11; i >= 0; i--)
                    {
                        ResultAdd += (long)(Add * (1 + Rate * i / 12));
                    }
                }


                N0 = ResultBase + ResultAdd;
            }

            return N0;
        }

        private void period_TextChanged(object sender, EventArgs e)
        {
            Add_Period = long.Parse(period_add.Text.Trim());
            Period = long.Parse(period.Text.Trim());

            Sub_Period = Period - Add_Period;
            period_sub.Text = Sub_Period.ToString();
        }

    }
}
